package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.ruoyi.system.domain.MesMatbom;
import com.ruoyi.system.domain.MesNeedMatMenu;
import com.ruoyi.system.domain.MesSordProd;
import com.ruoyi.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.MesPlan;
import com.ruoyi.system.service.IMesPlanService;

/**
 * 计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-01
 */
@Service
public class MesPlanServiceImpl implements IMesPlanService 
{
    @Autowired
    private MesPlanMapper mesPlanMapper;
    @Autowired
    private MesSordProdMapper mesSordProdMapper;
    @Autowired
    private MesProductMapper mesProductMapper;
    @Autowired
    private MesMatbomMapper mesMatbomMapper;
    @Autowired
    private MesStockMapper mesStockMapper;

    /**
     * 查询计划
     * 
     * @param id 计划主键
     * @return 计划
     */
    @Override
    public MesPlan selectMesPlanById(Integer id)
    {
        return mesPlanMapper.selectMesPlanById(id);
    }

    /**
     * 查询计划列表
     * 
     * @param mesPlan 计划
     * @return 计划
     */
    @Override
    public List<MesPlan> selectMesPlanList(MesPlan mesPlan)
    {
        return mesPlanMapper.selectMesPlanList(mesPlan);
    }

    /**
     * 新增计划
     * 
     * @param mesPlan 计划
     * @return 结果
     */
    @Override
    public int insertMesPlan(MesPlan mesPlan)
    {
        return mesPlanMapper.insertMesPlan(mesPlan);
    }

    /**
     * 修改计划
     * 
     * @param mesPlan 计划
     * @return 结果
     */
    @Override
    public int updateMesPlan(MesPlan mesPlan)
    {
        return mesPlanMapper.updateMesPlan(mesPlan);
    }

    /**
     * 批量删除计划
     * 
     * @param ids 需要删除的计划主键
     * @return 结果
     */
    @Override
    public int deleteMesPlanByIds(Integer[] ids)
    {
        return mesPlanMapper.deleteMesPlanByIds(ids);
    }

    /**
     * 删除计划信息
     * 
     * @param id 计划主键
     * @return 结果
     */
    @Override
    public int deleteMesPlanById(Integer id)
    {
        return mesPlanMapper.deleteMesPlanById(id);
    }

    @Override
    public boolean isPlanIdUnique(MesPlan mesPlan, Boolean addOrUpdate)
    {
        int count = mesPlanMapper.countByPlanId(mesPlan.getPlanId());
        if(addOrUpdate) {
            return count == 0;
        }
        else {
            if(mesPlan.getPlanId().equals(mesPlan.getOldPlanId())){
                return count == 1;
            }
            else {
                return count == 0;
            }
        }
    }

    @Override
    public List<MesNeedMatMenu> selectAllMat(String matPlan){

        //List<MesNeedMatMenu> mesNeedMatMenuList = null;
        List<MesNeedMatMenu> mesNeedMatMenuList = new ArrayList<>(); // 初始化为空ArrayList，可动态增长
        //销售订单
        String sordId = mesPlanMapper.selectSordId(matPlan);
        //产品 及其对应数量
        List<MesSordProd> productsList = mesSordProdMapper.selectProdIds(sordId);

        for (MesSordProd product : productsList) {

            //product.getProdId();//产品编码
            //产品编码拿到bom编码
            String bomI = mesProductMapper.selectBomId(product.getProdId());

            //bom编码 到 原料bom表 拿到 对应的原料编码及其数量
            List<MesMatbom> matsList = mesMatbomMapper.selectMatIds(bomI);
            //for (MesMatbom mat : matsList)
            for (int i = 0; i < matsList.size(); i++) {
                MesMatbom mat = matsList.get(i);
                //mat.getMatId();//原料编码
                //mat.getNum();//原料数量

                MesNeedMatMenu mesNeedMatMenu = new MesNeedMatMenu();

                mesNeedMatMenu.setMatId(mat.getMatId());//原料编码
                mesNeedMatMenu.setNeedMatNum((mat.getNum())*(product.getProdNum()));//订单需要的总原料

                //用原料编码 查当前库存
                int matNum = mesStockMapper.selectMatNum(mat.getMatId());
                mesNeedMatMenu.setWarehouseNum((long) matNum);
                mesNeedMatMenu.setNeedBuyNum((mat.getNum())*(product.getProdNum())-matNum);

                //mesNeedMatMenuList.set(i,mesNeedMatMenu);
                // 使用add方法而非set，因为set需要确切的索引位置且列表必须事先初始化并有足够的容量
                mesNeedMatMenuList.add(mesNeedMatMenu);
            }

            //product.getProdNum();//产品数量
        }
        if(mesNeedMatMenuList.size()>1){
            for(int i = 0; i < mesNeedMatMenuList.size(); i++){

                for(int j = i+1; j < mesNeedMatMenuList.size(); j++){
                    if(Objects.equals(mesNeedMatMenuList.get(i).getMatId(), mesNeedMatMenuList.get(j).getMatId())){

                        mesNeedMatMenuList.get(i).setNeedMatNum( (mesNeedMatMenuList.get(i).getNeedMatNum())+(mesNeedMatMenuList.get(j).getNeedMatNum()));
                        mesNeedMatMenuList.get(i).setNeedBuyNum((mesNeedMatMenuList.get(i).getNeedMatNum())-(mesNeedMatMenuList.get(i).getWarehouseNum()));
                        mesNeedMatMenuList.remove(mesNeedMatMenuList.get(j));
                    }
                }
            }
        }


        return mesNeedMatMenuList;
        //产品编码 拿到
    }
}
